package com.healthcloud.controller.modules.testresult;

import com.alibaba.fastjson.JSONObject;
import com.healthcloud.entity.*;
import com.healthcloud.entity.testresult.TestResultDto;
import com.healthcloud.entity.testresult.TestresultVo;
import com.healthcloud.service.*;
import com.healthcloud.utils.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;


/**
 * 检测结果
 * 
 */
@Controller
@RequestMapping("testresult")
public class TestresultController {

	@Autowired
    private TestresultService testresultService;

    @ApiOperation(value = "检测数据列表")
    @RequestMapping(value = {"/searchResult", "/n/searchResult", "/list"}, method = RequestMethod.POST)
    @ResponseBody
    public JSONObject searchResult(TestresultVo entity, @ModelAttribute("pageParam")PageParam pageParam){
        return searchResultVo(entity, pageParam);
    }

    @ApiIgnore
    private JSONObject searchResultVo(TestresultVo entity, PageParam pageParam){
        List<TestresultEntity> list = testresultService.queryListVo(entity, pageParam);
        int count = testresultService.queryTotalVo(entity);
        MsgVo msg = new MsgVo(list,count,count,pageParam.getsEcho());
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(msg));
        o.put("startDate", DateUtils.beforeDay(entity.getDate()));
        o.put("endDate", DateUtils.getDay(Calendar.DATE, 0));
        return o;
    }

    @ApiOperation(value = "血糖检测列表查询")
    @RequestMapping(value = {"/patientTests", "/n/patientTests"},method = RequestMethod.POST)
    @ResponseBody
    public MsgVo patientTests(TestresultVo entity, PageParam pageParam){
        List list = testresultService.queryPatientTests(entity, pageParam);
        int count = testresultService.queryPatientTestsCount(entity);
        return new MsgVo(list, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "新增检测信息", notes = "testTime:检测时间 idCard:身份证 testKindCode:[104:血压,105:血糖,502:血脂]" +
            "dataType:20 isPublish:1 testItemCode/testItemName:[502001/总胆固醇,502002/三酰甘油,502003/高密度脂蛋白胆固醇] " +
            "testValue:值")
    @RequestMapping(value = "/addTestResults", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes addBpInfo(@RequestBody List<TestresultEntity> results){
        for (TestresultEntity entity: results) {
            testresultService.save(entity);
        }
        return ApiRes.SUCCESS;
    }

    @ApiOperation(value = "血压检测列表查询")
    @RequestMapping(value = {"/patientBpTests", "/n/patientBpTests", "/n/queryPatientBpTests"},method = RequestMethod.POST)
    @ResponseBody
    public MsgVo patientBpTests(TestresultVo entity, PageParam pageParam){
        List list = testresultService.queryPatientBpTests(entity, pageParam);
        int count = testresultService.queryPatientBpTestsCount(entity);
        return new MsgVo(list, count, count, pageParam.getsEcho());
    }

    @ApiOperation(value = "血糖数据修改")
    @PutMapping("/updateBs")
    public ApiRes updateBs(@ApiParam(required = true, name = "id")
                      @RequestParam("id") Integer id,
                      @ApiParam(required = true, name = "testItemCode", value = "105001:空腹血糖 105002:餐后两小时")
                      @RequestParam("testItemCode") String testItemCode){
        String testItemName;
        String preCode = "105001";
        String afterCode = "105002";
        if (preCode.equals(testItemCode)){
            testItemName = "空腹血糖";
        }else if (afterCode.equals(testItemCode)){
            testItemName = "餐后2小时血糖";
        }else {
            throw new RRException("项目编码有误");
        }
        testresultService.updateBs(id, testItemCode, testItemName);
        return ApiRes.SUCCESS;
    }
}
